package com.dal.flink.userPurchaseBehaviorTracker.schema;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import org.apache.flink.api.common.typeinfo.TypeHint;
import org.apache.flink.api.common.typeinfo.TypeInformation;
import org.apache.flink.streaming.connectors.kafka.KafkaDeserializationSchema;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import com.dal.flink.userPurchaseBehaviorTracker.model.UserEvent;

/**
 * @Author: 🐟lifei🐟
 * @Date: 2019/1/27 下午9:08
 */
public class UserEventDeserializationSchema implements KafkaDeserializationSchema<UserEvent> {
//    @Override
//    public UserEvent deserialize(byte[] messageKey, byte[] message, String topic, int partition, long offset) throws IOException {
//        return JSON.parseObject(new String(message), new TypeReference<UserEvent>() {
//        });
//    }

    @Override
    public boolean isEndOfStream(UserEvent nextElement) {
        return false;
    }

    @Override
    public UserEvent deserialize(ConsumerRecord<byte[], byte[]> record) throws Exception {
        return JSON.parseObject(new String(record.value()), new TypeReference<UserEvent>() {
        });
    }

    @Override
    public TypeInformation<UserEvent> getProducedType() {
        return TypeInformation.of(new TypeHint<UserEvent>() {
        });
    }
}
